import type { Directive } from 'vue';

// 定义变量记录开始时间和结束时间
let timer: any;
let callback: null | Function;
function handleTouchStart() {
  // 获取当前时间并保存为开始时间
  timer = setTimeout(() => {
    callback && callback();
  }, 500);
}

// touchend事件处理函数
function handleTouchEnd() {
  timer && clearInterval(timer);
}
const vLongPress: Directive = {
  mounted(el, binding) {
    const input = el.querySelector('input');
    if (input) {
      input.addEventListener('touchstart', handleTouchStart);
      input.addEventListener('touchend', handleTouchEnd);
    }
    callback = typeof binding.value === 'function' ? binding.value : null;
    console.log('input: ', input);
    console.log('binding: ', binding);
  },
  beforeUnmount(el) {
    const input = el.querySelector('input');
    if (input) {
      input.removeEventListener('touchstart', handleTouchStart);
      input.removeEventListener('touchend', handleTouchEnd);
    }
  },
};

export default vLongPress;
